# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import scrapy
from scrapy.pipelines.images import ImagesPipeline


class SoImagePipeline(ImagesPipeline):
    # 重写get_media_requests()
    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            print(image_url)
            yield scrapy.Request(url=image_url, meta={'index': item['image_urls'].index(image_url), 'item': item})


    # 重写file_path()
    def file_path(self, request, response=None, info=None):
        # 获取item
        item = request.meta['item']
        # 获取索引
        index = request.meta['index']
        # 保存文件
        return 'full/%s.jpg' % (item['title'][index])
